package com.idbk.chargestation.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;

import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * 电桩信息（充电点下电桩列表）
 * @author liangzw,lupc, zhx
 *
 */
public class JsonPointPile implements Parcelable {

	public final static String KEY = JsonPointPile.class.getSimpleName();
	/**
	 * 充电时选中的枪号
	 */
	public final static int GUN_A = 1;
	public final static int GUN_B = 2;

	private int index = 0;
	/**
	 * 枪的状态
	 */
	public final static int STATUS_FAULT = 1;//故障
	public final static int STATUS_WARN = 2;//告警
	public final static int STATUS_IDLE = 3;//空闲
	public final static int STATUS_CHARGING = 4;//充电中
	public final static int STATUS_FINISH = 5;//完成
	public final static int STATUS_BESPEAK = 6;//预约中
	public final static int STATUS_WAIT = 7;//等待
	public final static int STATUS_OFFLINE = 10;//离线状态
	/**
	 * 桩的状态
	 */
	public final static int PILE_FAULT = 1;
	public final static int PILE_WARN = 2;
	public final static int PILE_IDLE = 3;
	public final static int PILE_CHARGING = 4;
	public final static int PILE_FINISH = 5;
	public final static int PILE_BESPEAK = 6;
	public final static int PILE_WAIT = 7;
	public final static int PILE_OFFLINE = 10;
	/**
	 * 地锁的状态
	 */
	public final static int STATUS_OFF_LINE = 0;
	public final static int STATUS_LOCK = 1;
	public final static int STATUS_UNLOCK = 2;
	public final static int STATUS_UNKNOW = 5;
	public final static int STATUS_NONE = 8;
	
	/**
	 * 直流充电桩
	 */
	public final static int PILE_TYPE_DC = 1;
	/**
	 * 单相交流
	 */
	public final static int PILE_TYPE_AC_SINGLE_PHASE = 2;
	/**
	 * 三相交流
	 */
	public final static int PILE_TYPE_AC_THREE_PHASE = 3;
	/**
	 * 直流双枪轮充
	 */
	public final static int PILE_TYPE_DC_DOUBLE_GUN_SYN = 4;
	/**
	 * 直流双枪同充
	 */
	public final static int PILE_TYPE_DC_DOUBLE_GUN_ASYN = 5;
	/**
	 * 交流双枪单相同充
	 */
	public final static int PILE_TYPE_AC_DOUBLE_GUN_SINGLE_PHASE= 6;
	/**
	 * 交流双枪三相同充
	 */
	public final static int PILE_TYPE_AC_DOUBLE_GUN_THREE_PHASE = 7;
	/**
	 * 多枪直流桩 超充
	 */
	public final static int PILE_TYPE_DC_MULTI_GUN_SYN = 8;
	/**
	 * 多枪单相交流桩 慢充
	 */
	public final static int PILE_TYPE_AC_MULTI_GUN_SINGLE_SYN = 9;
	/**
	 * 多枪三相交流桩 快充
	 */
	public final static int PILE_TYPE_AC_MULTI_GUN_THREE_SYN = 10;
	
	/**
	 * 电价类型：通用电价
	 */
	public final static int PRICE_TYPE_NORMAL = 1;
	/**
	 * 电价类型：分时电价
	 */
	public final static int PRICE_TYPE_TIME = 2;
	
	/**
	 * 充电停止条件：自动充满
	 */
	public final static int CHARGE_FINISH_BY_AUTO = 4;
	/**
	 * 充电停止条件：金额
	 */
	public final static int CHARGE_FINISH_BY_MONEY = 3;
	/**
	 * 充电停止条件：时间
	 */
	public final static int CHARGE_FINISH_BY_TIME = 2;
	/**
	 * 充电停止条件：电量
	 */
	public final static int CHARGE_FINISH_BY_ENERGY = 1;
	

	@SerializedName("pileNum")
	public int pileNum;//桩号

	@SerializedName("pileSn")
	public String pileSn;//充电桩序列号

	@SerializedName("performance")
	public String performance;//性能描述

	@SerializedName("pileType")
	public int pileType;//电桩类型

	@SerializedName("pileStatue")
	public int pileStatue;//电桩状态

	@SerializedName("gunA")
	public int gunA;//A枪口的状态

	@SerializedName("gunB")
	public int gunB;//B枪口状态

	@SerializedName("bespeakMinutes")
	public int bespeakMinutes;//最大可预约时间, 单位分钟

	@SerializedName("perMinutes")
	public int perMinutes;//预约费按时间收费

	@SerializedName("bookMoney")
	public double bookMoney;//预约单价,单位：元

	@SerializedName("priceTypeEnum")
	public int priceTypeEnum;//电价类型

	@SerializedName("isbook")
	public boolean isbook;//是否有预约

	@SerializedName("bookAvailable")
	public boolean bookAvailable;//该电桩是否支持预约

	@SerializedName("tariff")
	public JsonPileTariff tariff;//资费策略
	
	@SerializedName("isEnabled")
	public boolean isEnabled;//充电桩是否启用
	
	@SerializedName("groundLockNum")
	public int groundLockNum;//地锁数量
	
	@SerializedName("lock1")
	public int lock1;//地锁1
	
	@SerializedName("lock2")
	public int lock2;//地锁2

	@SerializedName("chargeRate")
	public String chargeRate;//充电桩速率

	@SerializedName("gunInfos")
	public List<JsonGunInfor> gunInfos;//充电枪属性

	/**
	 * 获取 电价类型 的字符串
	 * @return 电价类型的字符串
	 */
	public String getPriceTypeString(){
		if(priceTypeEnum == PRICE_TYPE_NORMAL){
			return "通用电价";
		} else if(priceTypeEnum == PRICE_TYPE_TIME){
			return "分时电价";
		} else {
			return "N/A";
		}
	}

	public static String getPileType(int type){
		String r = "";
		switch (type) {
		case 1:
			r= "直流充电桩";
			break;
		case 2:
			r= "单相交流桩";
			break;
		case 3:
			r= "三相交流桩";
			break;
		case 4:
			r= "双枪轮流充直流桩";
			break;
		case 5:
			r= "双枪同时充直流桩";
			break;
		case 6:
			r= "双枪单相同充交流桩";
			break;
		case 7:
			r= "双枪三相同充交流桩";
			break;
		default:
			r = "未知类型";
			break;
		}
		return r;
	}

	public JsonPointPile(){

	}

	public String pileInfor() {
		String infor;
		String STR_PILE_INFO_1 =
				"<big>#%02d</big><br/><br/>" +
						"<small>桩序列号：%s<br/>" +
						"当前费用：%s<br/>" +
						"电价类型：%s<br/>" +
						"电价信息：<font color='#f1a225'>%s</font>元/度</small>";
		String STR_PILE_INFO_2 =
				"<big>#%02d</big><br/><br/>" +
						"<small>桩序列号：%s<br/>" +
						"当前费用：%s<br/>" +
						"电价类型：%s<br/>" +
						"分时信息：</small><br/>";

		if (tariff == null) {
			infor = "非官方电桩不支持显示资费信息";

		} else if (priceTypeEnum == JsonPointPile.PRICE_TYPE_NORMAL) {
			infor = String.format(Locale.CHINA, STR_PILE_INFO_1,
					pileNum,
					pileSn,
					getInfor(),
					getPriceTypeString(),
					tariff.getCommonPriceString()
			);
			if (groundLockNum > 0) {
				infor += "<small><br/>" + "地锁数量：" + groundLockNum + "</small>";
			}
			double parkCost = tariff.parkingFee * 0.01;
			infor += "<small><br/>" + "停车费用：" + String.format(Locale.CHINA, "%.2f", parkCost)
					+ " 元/每10分钟" + "</small>";
		} else if (priceTypeEnum == JsonPointPile.PRICE_TYPE_TIME) {
			infor = String.format(Locale.CHINA, STR_PILE_INFO_2,
					pileNum,
					pileSn,
					getInfor(),
					getPriceTypeString()
			);
			StringBuffer sb = new StringBuffer(infor);
			String[] timeSpans = new String[]{
					tariff.time1StartAt, tariff.time2StartAt, tariff.time3StartAt,
					tariff.time4StartAt, tariff.time5StartAt, tariff.time6StartAt,
					tariff.time7StartAt, tariff.time8StartAt, tariff.time9StartAt,
					tariff.time10StartAt, tariff.time11StartAt, tariff.time12StartAt,
			};
			int[] prices = new int[]{
					tariff.time1PriceCode, tariff.time2PriceCode, tariff.time3PriceCode,
					tariff.time4PriceCode, tariff.time5PriceCode, tariff.time6PriceCode,
					tariff.time7PriceCode, tariff.time8PriceCode, tariff.time9PriceCode,
					tariff.time10PriceCode, tariff.time11PriceCode, tariff.time12PriceCode,
			};
			//
			for (int i = 0; i < timeSpans.length; i++) {
				if (timeSpans[0] == null) {
					break;
				}
				//最后一个
				if (i == timeSpans.length - 1) {
					String start = tariff.getSplitTimeString(timeSpans[i]);
					String end = "24:00";
					appendPriceInfo(sb, start, end, prices[i]);
				} else {
					if (timeSpans[i + 1] == null) {
						String start = tariff.getSplitTimeString(timeSpans[i]);
						String end = "24:00";
						appendPriceInfo(sb, start, end, prices[i]);
						break;
					} else {
						String start = tariff.getSplitTimeString(timeSpans[i]);
						String end = tariff.getSplitTimeString(timeSpans[i + 1]);
						appendPriceInfo(sb, start, end, prices[i]);
					}
				}
			}

			infor = sb.toString();
			if (groundLockNum > 0) {
				infor += "<small><br/>" + "地锁数量：" + groundLockNum + "</small>";
			}
			double parkCost = tariff.parkingFee * 0.01;
			infor += "<small><br/>" + "停车费用：" + String.format(Locale.CHINA, "%.2f", parkCost) + " 元/每10分钟" + "</small>";

		} else {
			infor = "暂不支持显示该资费策略";
		}
		return infor;
	}

	public String gunInfor(int num) {
		String infor;
		String STR_PILE_INFO_1 =
				"<big>枪%2d</big><br/>" +
						"<small>当前费用：%s<br/>" +
						"电价类型：%s<br/>" +
						"电价信息：<font color='#f1a225'>%s</font>元/度</small>";
		String STR_PILE_INFO_2 =
				"<big>枪%2d</big><br/>" +
						"<small>当前费用：%s<br/>" +
						"电价类型：%s<br/>" +
						"分时信息：</small><br/>";

		if (tariff == null) {
			infor = "非官方电桩不支持显示资费信息";

		} else if (priceTypeEnum == JsonPointPile.PRICE_TYPE_NORMAL) {
			infor = String.format(Locale.CHINA, STR_PILE_INFO_1,
					num,
					getInfor(),
					getPriceTypeString(),
					tariff.getCommonPriceString()
			);
			if (groundLockNum > 0) {
				infor += "<small><br/>" + "地锁数量：" + groundLockNum + "</small>";
			}
			double parkCost = tariff.parkingFee * 0.01;
			infor += "<small><br/>" + "停车费用：" + String.format(Locale.CHINA, "%.2f", parkCost)
					+ " 元/每10分钟" + "</small>";
		} else if (priceTypeEnum == JsonPointPile.PRICE_TYPE_TIME) {
			infor = String.format(Locale.CHINA, STR_PILE_INFO_2,
					num,
					getInfor(),
					getPriceTypeString()
			);
			StringBuffer sb = new StringBuffer(infor);
			String[] timeSpans = new String[]{
					tariff.time1StartAt, tariff.time2StartAt, tariff.time3StartAt,
					tariff.time4StartAt, tariff.time5StartAt, tariff.time6StartAt,
					tariff.time7StartAt, tariff.time8StartAt, tariff.time9StartAt,
					tariff.time10StartAt, tariff.time11StartAt, tariff.time12StartAt,
			};
			int[] prices = new int[]{
					tariff.time1PriceCode, tariff.time2PriceCode, tariff.time3PriceCode,
					tariff.time4PriceCode, tariff.time5PriceCode, tariff.time6PriceCode,
					tariff.time7PriceCode, tariff.time8PriceCode, tariff.time9PriceCode,
					tariff.time10PriceCode, tariff.time11PriceCode, tariff.time12PriceCode,
			};
			//
			for (int i = 0; i < timeSpans.length; i++) {
				if (timeSpans[0] == null) {
					break;
				}
				//最后一个
				if (i == timeSpans.length - 1) {
					String start = tariff.getSplitTimeString(timeSpans[i]);
					String end = "24:00";
					appendPriceInfo(sb, start, end, prices[i]);
				} else {
					if (timeSpans[i + 1] == null) {
						String start = tariff.getSplitTimeString(timeSpans[i]);
						String end = "24:00";
						appendPriceInfo(sb, start, end, prices[i]);
						break;
					} else {
						String start = tariff.getSplitTimeString(timeSpans[i]);
						String end = tariff.getSplitTimeString(timeSpans[i + 1]);
						appendPriceInfo(sb, start, end, prices[i]);
					}
				}
			}

//			String in = "<small>"+sb+"</small>";
			infor = sb.toString();

			if (groundLockNum > 0) {
				infor += "<small><br/>" + "地锁数量：" + groundLockNum + "</small>";
			}
			double parkCost = tariff.parkingFee * 0.01;
			infor += "<small><br/>" + "停车费用：" + String.format(Locale.CHINA, "%.2f", parkCost) + " 元/每10分钟" + "</small>";

		} else {
			infor = "暂不支持显示该资费策略";
		}
		return infor;
	}
	//计算当前电价
	public String getInfor(){
		StringBuffer sb = new StringBuffer();
		String price;
		String[] timeSpans = new String[]{
				tariff.time1StartAt, tariff.time2StartAt, tariff.time3StartAt,
				tariff.time4StartAt, tariff.time5StartAt, tariff.time6StartAt,
				tariff.time7StartAt, tariff.time8StartAt, tariff.time9StartAt,
				tariff.time10StartAt, tariff.time11StartAt, tariff.time12StartAt,
		};
		int[] prices = new int[]{
				tariff.time1PriceCode, tariff.time2PriceCode, tariff.time3PriceCode,
				tariff.time4PriceCode, tariff.time5PriceCode, tariff.time6PriceCode,
				tariff.time7PriceCode, tariff.time8PriceCode, tariff.time9PriceCode,
				tariff.time10PriceCode, tariff.time11PriceCode, tariff.time12PriceCode,
		};
		//
		for (int i = 0; i < timeSpans.length; i++) {
			if (timeSpans[0] == null) {
				break;
			}
			//最后一个
			if (i == timeSpans.length - 1) {
				String start = tariff.getSplitTimeString(timeSpans[i]);
				String end = "24:00";
				index = priceInfo(sb, start, end, prices[i]);
			} else {
				if (timeSpans[i + 1] == null) {
					String start = tariff.getSplitTimeString(timeSpans[i]);
					String end = "24:00";
					index = priceInfo(sb, start, end, prices[i]);
					break;
				} else {
					String start = tariff.getSplitTimeString(timeSpans[i]);
					String end = tariff.getSplitTimeString(timeSpans[i + 1]);
					index = priceInfo(sb, start, end, prices[i]);
				}
			}
		}
		if (priceTypeEnum == JsonPointPile.PRICE_TYPE_NORMAL) {
			price = tariff.getCommonPriceString() + "元/度 " +
					"服务费:" + tariff.getSplitTimeServicePriceInfo(index) + tariff.getServiceType();
		}else if (priceTypeEnum == JsonPointPile.PRICE_TYPE_TIME){
			price = tariff.getSplitTimePriceInfo(index) + "元/度 " +
					"服务费:" + tariff.getSplitTimeServicePriceInfo(index) + tariff.getServiceType();
		}else {
			price = tariff.getSplitTimePriceInfo(index) + "元/度 " +
					"服务费:" + tariff.getSplitTimeServicePriceInfo(index) + tariff.getServiceType();
		}
		return price;

	}
	private int priceInfo (StringBuffer sb, String start, String end, int priceIndex) {
		int start1 = Integer.parseInt(start.replace(":",""));
		int end1 = Integer.parseInt(end.replace(":",""));
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		String hour1 = ""+hour;
		String minute1 = ""+minute;
		if (minute1.length() == 1){
			minute1 = "0"+minute;
		}
		String date = hour1+minute1;
		int date1 = Integer.parseInt(date);
		if (start1 <= date1&& date1 <= end1){
			index = priceIndex;
		}
		return index;
	}

	private void appendPriceInfo(StringBuffer sb, String start, String end, int priceIndex) {
//		sb.append("&emsp;&emsp;&emsp;&emsp;");
		sb.append("<small>"+start+"</small>");
		sb.append("-");
		sb.append("<small>"+end+"</small>");
		sb.append("<small>"+" 电费:"+"</small>");
		sb.append(" <font color='#f1a225'>");
		sb.append("<small>"+tariff.getSplitTimePriceInfo(priceIndex)+"</small>");
		sb.append("<small></font>元/度<br/></small>");
//		sb.append("&emsp;&emsp;&emsp;");
		sb.append("<small>"+" 服务费:"+"</small>");
		sb.append(" <font color='#f1a225'>");
		sb.append("<small>"+tariff.getSplitTimeServicePriceInfo(priceIndex)+"</small>");
		sb.append("<small></font>"+tariff.getServiceType()+"<br/></small>");

		int start1 = Integer.parseInt(start.replace(":",""));
		int end1 = Integer.parseInt(end.replace(":",""));
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		String hour1 = ""+hour;
		String minute1 = ""+minute;
		String date = hour1+minute1;
		int date1 = Integer.parseInt(date);
		if (start1 <= date1&& date1 <= end1){

		}
	}

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.pileNum);
        dest.writeString(this.pileSn);
        dest.writeString(this.performance);
        dest.writeInt(this.pileType);
        dest.writeInt(this.pileStatue);
        dest.writeInt(this.gunA);
        dest.writeInt(this.gunB);
        dest.writeInt(this.bespeakMinutes);
        dest.writeInt(this.perMinutes);
        dest.writeDouble(this.bookMoney);
        dest.writeInt(this.priceTypeEnum);
        dest.writeByte(this.isbook ? (byte) 1 : (byte) 0);
        dest.writeByte(this.bookAvailable ? (byte) 1 : (byte) 0);
        dest.writeParcelable(this.tariff, flags);
        dest.writeByte(this.isEnabled ? (byte) 1 : (byte) 0);
        dest.writeInt(this.groundLockNum);
        dest.writeInt(this.lock1);
        dest.writeInt(this.lock2);
        dest.writeString(this.chargeRate);
        dest.writeTypedList(this.gunInfos);
    }

    protected JsonPointPile(Parcel in) {
        this.pileNum = in.readInt();
        this.pileSn = in.readString();
        this.performance = in.readString();
        this.pileType = in.readInt();
        this.pileStatue = in.readInt();
        this.gunA = in.readInt();
        this.gunB = in.readInt();
        this.bespeakMinutes = in.readInt();
        this.perMinutes = in.readInt();
        this.bookMoney = in.readDouble();
        this.priceTypeEnum = in.readInt();
        this.isbook = in.readByte() != 0;
        this.bookAvailable = in.readByte() != 0;
        this.tariff = in.readParcelable(JsonPileTariff.class.getClassLoader());
        this.isEnabled = in.readByte() != 0;
        this.groundLockNum = in.readInt();
        this.lock1 = in.readInt();
        this.lock2 = in.readInt();
        this.chargeRate = in.readString();
        this.gunInfos = in.createTypedArrayList(JsonGunInfor.CREATOR);
    }

    public static final Creator<JsonPointPile> CREATOR = new Creator<JsonPointPile>() {
        @Override
        public JsonPointPile createFromParcel(Parcel source) {
            return new JsonPointPile(source);
        }

        @Override
        public JsonPointPile[] newArray(int size) {
            return new JsonPointPile[size];
        }
    };
}
